<template>
    <BasicModal
      v-bind="$attrs"
      @register="registerModal"
      show-footer
      width="60%"
      @ok="handleSubmit"
    >
      <template #title>
        {{ isUpdate ? '编辑' : '新增' }}
      </template>
      <template #footer>
        <a-button @click="closeModal" :loading="btnLoading">取消</a-button>
        <a-button
          type="primary"
          @click="handleSubmit(false)"
          :loading="btnLoading"
          >保存</a-button
        >
      </template>
      <BasicForm @register="registerForm">
      </BasicForm>
    </BasicModal>
  </template>
  <script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form/index';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useThrottleFn } from '@vueuse/core';
  import { defHttp } from '@/utils/http/axios';
  const emit = defineEmits(['success', 'register']);
  let isUpdate = ref(true);
  const btnLoading = ref(true);
  const { createMessage } = useMessage();
  const [
    registerForm,
    { resetFields, setFieldsValue, validate },
  ] = useForm({
    labelWidth: 150,
    baseColProps: { span: 24 },
    schemas: [
      {
        field: 'id',
        component: 'Input',
        label: 'id',
        show: false,
      },
      {
        field: 'name',
        component: 'Input',
        label: '充电站名称',
        required: true,
      },
      {
        field: 'terminalTypeId',
        component: 'ApiSelect',
        label: '类型',
        required: true,
        ifShow: ({ values }) => !values.id, 
        componentProps: {
          api: '/admin/main/terminalTypeList',
          labelField: 'name',
          valueField: 'id',
          clearable: true,
        },
      },
      {
        field: 'protocolType',
        component: 'ApiSelect',
        label: '协议',
        required: true,
        ifShow: ({ values }) => !values.id,
        componentProps: {
          options: [
            { label: '万马', value: 0 },
            { label: '特来电', value: 1 },
            { label: '云快充', value: 10 },
          ],
        },
      },
      {
        field: 'showUrl',
        component: 'AvUpload',
        label: '预览图',
        componentProps: {
          maxCount: 1,
        }
      },
      {
        field: 'stationSn',
        component: 'Input',
        label: '充电站编号',
        required: true,
      },
      {
        field: 'areaId',
        component: 'AreaSelect',
        label: '城市',
        required: true,
      },
      {
        field: 'address',
        component: 'Input',
        label: '详细地址',
        required: true,
      },
      {
        field: 'powerUnit',
        component: 'Input',
        label: '功率',
        required: true,
      },
      {
        field: 'chargeStandardId',
        component: 'ApiSelect',
        label: '默认计费标准',
        required: true,
        componentProps: {
          api: '/admin/main/chargeStandardList',
          labelField: 'name',
          valueField: 'id',
          showSearch: true,
          allowClear: true,
        }
      },
      {
        field: 'mpChargeStandardId',
        component: 'ApiSelect',
        label: '小程序计费标准',
        required: true,
        componentProps: {
          api: '/admin/main/chargeStandardList',
          labelField: 'name',
          valueField: 'id',
          showSearch: true,
          allowClear: true,
        }
      },
      {
        field: 'terminalTypeId',
        component: 'ApiSelect',
        label: '终端名称',
        required: true,
        componentProps: {
          api: '/admin/main/terminalTypeList',
          labelField: 'name',
          valueField: 'id',
          showSearch: true,
          allowClear: true,
        }
      },
      {
        field: 'operatorId',
        component: 'ApiSelect',
        label: '电桩运营商',
        required: true,
        componentProps: {
          api: '/admin/main/operatorList',
          labelField: 'memo',
          valueField: 'id',
          showSearch: true,
          allowClear: true,
        }
      },
      {
        field: 'showUrls',
        component: 'AvUpload',
        label: '多图上传',
      },
    ],
    showActionButtonGroup: false,
  });
  const [registerModal, { closeModal, changeLoading }] =
  useModalInner(async (data) => {
      resetFields();
      changeLoading(true);
      btnLoading.value = true;
      isUpdate.value =!!data?.isUpdate;
      if (isUpdate.value) {
        setFieldsValue({ ...data.record });
      }
      changeLoading(false);
      btnLoading.value = false;
    });
  const handleSubmit = useThrottleFn(doSubmit, 1000);
  async function doSubmit(hasNext: boolean) {
    try {
      changeLoading(true);
      btnLoading.value = true;
      const values = await validate();
      if (isUpdate.value) {
        const res = await defHttp.post({ url: '/admin/station/yu_edit', data: values });
        createMessage.success(res);
      } else {
        const res = await defHttp.post({ url: '/admin/station/yu_add', data: values });
        createMessage.success(res);
      }
      emit('success');
      closeModal();
    } finally {
      btnLoading.value = false;
      changeLoading(false);
    }
  }
  </script>
  